package com.zkthink.ceres.promotion.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.ceres.promotion.dto.SalesOrderPageDTO;
import com.zkthink.ceres.promotion.dto.SalesOrderUpdateDTO;
import com.zkthink.ceres.promotion.entity.SalesOrder;
import com.zkthink.ceres.promotion.service.SalesOrderService;
import com.zkthink.ceres.promotion.vo.admin.SalesOrderPageVO;
import com.zkthink.security.annotation.PreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器 分销员
 * </p>
 *
 * @author kellen
 * @date 2020-08-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/salesOrder")
@Api(value = "分销订单", tags = "分销订单")
//@PreAuth(replace = "salesOrder:")
@AllArgsConstructor
public class SalesOrderController {

  private SalesOrderService salesOrderService;


  /**
   * 分页查询分销订单
   */
  @ApiOperation("分页查询分销订单")
  @PostMapping("/page")
  public R<IPage<SalesOrderPageVO>> page(@RequestBody PageParams<SalesOrderPageDTO> params) {
    return R.success(salesOrderService.page(params));
  }


  /**
   * 手工结算
   */
  @ApiOperation("手工结算")
  @PostMapping("/updateSettleStatus")
  public R updateSettleStatus(@RequestBody SalesOrderUpdateDTO salesOrderUpdateDTO) {
    List<Long> ids = salesOrderUpdateDTO.getIds();
    if (CollectionUtils.isEmpty(ids)) {
      return R.validFail("订单id不允许为空");
    }
    List<SalesOrder> salesOrderList = Lists.newArrayList();
    if (!CollectionUtils.isEmpty(ids)) {
      ids.forEach(e -> salesOrderList.add(SalesOrder.builder().id(e).settleStatus(2).build()));
    }
    salesOrderService.updateBatchById(salesOrderList);
    return R.success();
  }


}
